package org.timeismoney.payment.service.impl;

import java.util.List;

import org.timeismoney.payment.common.PageBean;
import org.timeismoney.payment.dao.StudentDao;
import org.timeismoney.payment.exception.SysException;
import org.timeismoney.payment.model.SchoolYear;
import org.timeismoney.payment.model.Student;
import org.timeismoney.payment.service.ItemStuDetailService;
import org.timeismoney.payment.service.PaymentFinService;
import org.timeismoney.payment.service.PaymentRecordService;
import org.timeismoney.payment.service.StudentService;

public class StudentServiceImpl extends BaseServiceImpl<Student, String>
		implements StudentService {
	private PaymentRecordService paymentRecordService;
	private PaymentFinService finService;
	private ItemStuDetailService itemStuDetailService;

	public void setItemStuDetailService(ItemStuDetailService itemStuDetailService) {
		this.itemStuDetailService = itemStuDetailService;
	}

	public void setFinService(PaymentFinService finService) {
		this.finService = finService;
	}

	public void setPaymentRecordService(PaymentRecordService paymentRecordService) {
		this.paymentRecordService = paymentRecordService;
	}

	public Student validate(String stuNo, String pwd) {
		Student student = getDao().getEntity(stuNo.trim());
		if (student != null) {
			if (student.getPwd().equals(pwd)) {
				return student;
			}
		}
		return null;
	}

	public void findStuByUnit(int unitNo, Integer schoolYearNo,
			PageBean<Student> pageBean) {
		getDao().find(
				"from Student s left join fetch s.unit u where u.unitNo=? order by s.stuNo asc",
				pageBean, unitNo);
	}

	public List<Student> findStuByUnit(int unitNo) {
		return getDao().find("from Student s where s.unit.unitNo=? order by s.stuNo asc", unitNo);
	}

	public void batchInsertStudents(List<Student> uploadList) {
		try {
			((StudentDao)getDao()).batchInsertStudents(uploadList);
		} catch (RuntimeException e) {throw new SysException("学号重复，请核实导入文件",e);}
	}

	@Override
	public void delete(Student e) {
		paymentRecordService.deleteByStuNo(e.getStuNo());
		finService.deleteByStuNo(e.getStuNo());
		itemStuDetailService.deleteByStuNo(e.getStuNo());
		super.delete(e);
	}

	public List getStuPaymentReport(SchoolYear schoolYear) {
		return ((StudentDao)getDao()).getStuPaymentReport(schoolYear);
	}

}
